<%@Page Language="C#" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Net" %>

<script runat="server">

	Regex _searchExpression;
	
	string _HtmlContent = null;
	
	void Page_Load( object Sender, EventArgs e )
	{
		Status.InnerText = "";

		if ( Page.IsPostBack )
		{
			HttpWebRequest myRequest; 
			HttpWebResponse myResponse; 
			Stream s;

			
			//myRequest = (HttpWebRequest) WebRequestFactory.Create(URLToRead.Value);
			myRequest = (HttpWebRequest) WebRequest.Create(URLToRead.Value);
			
			try
			{
				myResponse = (HttpWebResponse) myRequest.GetResponse();

				s = myResponse.GetResponseStream();
				_HtmlContent = new StreamReader(s).ReadToEnd();
				s.Close();

				Status.InnerText = "Read URL OK";
               
			}
			catch( WebException WebEx )
			{
				Status.InnerText = "WebException thrown";
				myResponse = (HttpWebResponse) WebEx.Response;
				
			}
			catch(Exception ex)
			{
				throw ex;
			}


			// Update table with response info
			
			ContentLength.InnerText = myResponse.ContentLength.ToString();
			ContentEncoding.InnerText = myResponse.ContentEncoding;
			ContentTypeInfo.InnerText = myResponse.ContentType;
			Server.InnerText = myResponse.Server;
			StatusCode.InnerText = myResponse.StatusCode.ToString();
			StatusDescription.InnerText = myResponse.StatusDescription;
			ResponseURL.InnerText = myResponse.ResponseUri.ToString();
			Method.InnerText = myResponse.Method;
			LastModified.InnerText = myResponse.LastModified.ToString();
			ProtocolVersion.InnerText = myResponse.ProtocolVersion.ToString();

		}
	}

	void RenderStreamIntoPage()
	{
		// Stream the return string for the expected ranking
			
		Regex re;
		Match m;
		
		re = new Regex("(?<x>Amazon.com Sales Rank: </b>.*\n)(?<rank>.*)");
		m = re.Match( _HtmlContent );

		// Check for multiple matches
						
		while( m.Success == true )
		{
			foreach( Capture c in m.Captures )
			{
				Response.Write("<BR>Ranking : " + m.Result("${rank}" ) );
			}
			m = m.NextMatch();
		}			
					
	}
	
</script>

<html>
<body>
<H1>Amazon.COM Sales Rank</H1>
<p>This example downloads a web page from Amazon.COM and extracts the sales ranking for a book.</p>
<form id="myform" runat="server">
	<table>
	<tr>
		<td>URL to read:
		<td><input type="text" size=60 id="URLToRead" runat="server" value="http://www.amazon.com/exec/obidos/ASIN/1861004885">
	<tr>
		<td colspan=2><input type="submit" Value="Read" runat="server">
		
	</table>
	
	<p id="Status" style="color:red" runat="server"></p>
</form>


<table>
<tr>
	<td>Server</td><td runat="server" id="Server" maintainstate="false"/>
</tr>
<tr>
	<td>ConnentLength</td><td runat="server" id="ContentLength" maintainstate="false"/>
</tr>
<tr>
	<td>ContentEncoding</td><td runat="server" id="ContentEncoding" maintainstate="false" />
</tr>
<tr>
	<td>ContentType</td><td runat="server" id="ContentTypeInfo"  maintainstate="false"/>
</tr>
<tr>
	<td>Status</td><td runat="server" id="StatusCode" maintainstate="false"/>
</tr>
<tr>
	<td>StatusDescription</td><td runat="server" id="StatusDescription" maintainstate="false"/>
</tr>
<tr>
	<td>ResponseURL</td><td runat="server" id="ResponseURL" maintainstate="false"/>
</tr>
<tr>
	<td>Method</td><td runat="server" id="Method" maintainstate="false"/>
</tr>
<tr>
	<td>LastModified</td><td runat="server" id="LastModified" maintainstate="false"/>
</tr>
<tr>
	<td>ProtocolVersion</td><td runat="server" id="ProtocolVersion" maintainstate="false"/>
</tr>
</table>

<HR>

<pre>
<%RenderStreamIntoPage(); %>
</pre>
</body>
</html>


